﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApp.BaseDataSvc;
using YY.Common;

namespace WebApp
{
    public partial class OptionFrm : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(OptionFrm), this);
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<int> SaveOption(int id, string type, string name)
        {
            AjaxResult<int> result = new AjaxResult<int>();
            result.Data = 0;
            result.Msg = null;
            bool update = id > 0;
            try
            {
                if (!this.IsPermitOP(EnumCommon.PermissionOpType.Delete, this.GetType().Name))
                {
                    result.Msg = "没有操作权限！";
                    return result;
                }

                TB_Options model = new TB_Options();
                if (update)
                    model = this.BaseDataProxy.GetOption(id);

                WebCommon.EvalControlValueToData(model, "type", type);
                WebCommon.EvalControlValueToData(model, "isSys", false);
                WebCommon.EvalControlValueToData(model, "parentId", 0);
                WebCommon.EvalControlValueToData(model, "name", name);

                if (update)
                {
                    this.BaseDataProxy.UpdateOption(model);
                    result.Data = model.id;
                }
                else
                {
                    result.Data = this.BaseDataProxy.AddOption(model);
                }
            }
            catch (Exception ex)
            {
                result.Data = 0;
                result.Msg = ex.Message;
            }
            return result;
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<bool> DeleteOption(int id)
        {
            AjaxResult<bool> result = new AjaxResult<bool>();
            result.Data = false;
            result.Msg = null;
            try
            {
                if (!this.IsPermitOP(EnumCommon.PermissionOpType.Delete, this.GetType().Name))
                {
                    result.Msg = "没有操作权限！";
                    return result;
                }

                result.Data = this.BaseDataProxy.DeleteOption(id);
            }
            catch (Exception ex)
            {
                result.Data = false;
                result.Msg = ex.Message;
            }
            return result;
        }
    }
}
